//
//  XMSetView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/13.
//

#import "XMSetView.h"
#import "SDWebImage/SDImageCache.h"

@interface XMSetView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation XMSetView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF0F0F0);
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xF0F0F0);
    self.navTitleLabel.text = @"设置";
    _titleArr = @[@[@"账号与安全"/*, @"通知设置", @"隐私设置"*/], @[@"我的地址", @"清理缓存", @"意见反馈", @"检测更新", @"退出登录"]];
    [self addSubview:self.tbl];
    
    UILabel *versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.height - KScaleWidth(70.0) - KScaleWidth(36.0), self.width, KScaleWidth(36.0)) font:KFontSize(16.0) textColor:kColorHEX(0x999999) textAlignment:NSTextAlignmentCenter];
    versionLabel.text = [NSString stringWithFormat:@"西梅玖境Version %@", kVersion];
    [self addSubview:versionLabel];
}

#pragma mark - Init
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *subArr = _titleArr[section];
    return subArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(8.0))];
    headerView.backgroundColor = kColorHEX(0xF0F0F0);
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(44.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMSetCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = KFontSize(17.0);
        cell.textLabel.textColor = kColorHEX(0x333333);
        cell.detailTextLabel.font = KFontSize(15.0);
        cell.detailTextLabel.textColor = kColorHEX(0x666666);
    }
    cell.textLabel.text = _titleArr[indexPath.section][indexPath.row];
    cell.detailTextLabel.text = @"";
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
//            NSUInteger bytesCache = [[SDImageCache sharedImageCache] totalDiskSize];
            NSUInteger bytesCache = [[SDImageCache sharedImageCache] getSize];
            //换算成 MB (注意iOS中的字节之间的换算是1000不是1024)
            float MBCache = bytesCache / 1000.0 / 1000.0;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", MBCache];//@"124MB";
        }
        else if (indexPath.row == 3) {
            cell.detailTextLabel.text = @"1.1.0";
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.itemSelectBlock ? self.itemSelectBlock(indexPath.section, indexPath.row) : nil;
    if (indexPath.section == 1 && indexPath.row == 1) {
        [[SDImageCache sharedImageCache] clearMemory];
        XMWeakSelf;
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [MBProgressHUD show:@"清理完成" icon:@"" view:weakSelf];
            [tableView reloadData];
        }];
    }
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH - KScaleWidth(106.0))];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        _tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
//        [_tbl registerClass:[UITableViewCell class] forCellReuseIdentifier:@"XMSetCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
